package com.guigu.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guigu.pojo.Product;
import com.guigu.pojo.ProductImg;
import com.guigu.pojo.UserAddress;
import com.guigu.service.ProductImgService;
import com.guigu.service.ProductService;
import com.guigu.utils.FileUploadUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.*;

/**
 * <p>
 * 商品表 前端控制器
 * </p>
 *
 * @author Mr.Zhang
 * @since 2025-03-12
 */
@CrossOrigin
@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private ProductService productService;

    @Autowired
    private ProductImgService productImgService;

    @PostMapping("/getAllProduct")
    @ResponseBody
    public Page<Product> getAllProduct(@RequestBody HashMap<String, Object> map) {
        Page<Product> product = productService.getProduct(map);
        System.out.println(product);
        if (product != null) {
            return product;
        }
        return null;
    }
    @GetMapping("/deleteProduct/{productId}")
    @ResponseBody
    public Boolean deleteProduct(@PathVariable Integer productId) {
        System.out.println("productId: " + productId);
        boolean b = productService.removeById(productId);
        return b;
    }

    @PostMapping("/batchDeleteProduct")
    @ResponseBody
    public Boolean batchDeleteProduct(@RequestBody List<Integer> productId) {
        System.out.println("productId: " + productId);
        boolean b = productService.removeByIds(productId);
        return b;
    }


    @PostMapping("/getProducts")
    @ResponseBody
    public Page<Product> getProducts(@RequestBody HashMap<String,Object> map){
        Page<Product> page = productService.getProducts(map);
        return page;
    }

    @GetMapping("/getProductById")
    @ResponseBody
    public Product getProductById(Integer productId){
        Product product = productService.getById(productId);
        return product;
    }

    @GetMapping("/getSellingProduct")
    @ResponseBody
    public List<Product> getSellingProduct(){
        List<Product> products = productService.getSellingProduct();
        return products;
    }

    @PostMapping("/getProductBySearchData")
    @ResponseBody
    public Page<Product> getProductBySearchData(@RequestBody HashMap<String,Object> map){
        Page<Product> page = productService.getProductBySearchData(map);
        return page;
    }

    @PostMapping("/saveProduct")
    @ResponseBody
    public Boolean saveProduct(@RequestParam("files") MultipartFile[] files, Product product){
        // 创建用户对象并设置用户名和密码
        System.out.println("files:"+files);
        System.out.println("product:"+product);

        // 设置文件上传的基础路径，并根据日期生成子路径
        String basePathStr = "C:\\Users\\Mr.Zhang\\document\\upload";
        String subPath = FileUploadUtils.makeUploadDirectoryByDate();
        basePathStr += subPath; // 文件上传的地址

        // 检查基础路径是否存在，如果不存在则创建
        File basePath = new File(basePathStr);
        if (!basePath.exists()) {
            basePath.mkdirs();
        }
        ArrayList<String> imgLinkList = new ArrayList<>();
        try {
            // 遍历上传的文件数组，重命名文件并保存到指定路径
            for (MultipartFile f : files) {
                String newFileName = FileUploadUtils.rename(f.getOriginalFilename());
                f.transferTo(new File(basePath, newFileName));
                imgLinkList.add(subPath + newFileName);// 设置用户头像路径
            }
            product.setImgLink(imgLinkList.get(0));
        } catch (Exception e) {
            throw new RuntimeException(e); // 捕获并抛出异常
        }

        boolean b = productService.save(product);
        if (b) {
            ProductImg productImg = new ProductImg();
            for (int i = 1; i < imgLinkList.size(); i++) {
                productImg.setImgLink(imgLinkList.get(i));
                productImg.setProductId(product.getProductId());
                productImg.setType(2);
                boolean save = productImgService.save(productImg);
            }
        }else {
            return false;
        }
        return true;
    }

}

